package com.hss01248.dialog.view;

import com.hss01248.dialog.WindowAdjuster;
import ohos.aafwk.ability.Ability;

import com.hss01248.dialog.config.ConfigBean;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.ComponentParent;
import ohos.event.commonevent.CommonEventManager;

/**
 * Created by Administrator on 2017/11/19.
 */

public class DialogUtil_DialogActivity extends Ability {

    ConfigBean bean;

    private ComponentContainer getTopWindow(Component com)
    {
        ComponentContainer ret = null;
        ComponentParent parent = com.getComponentParent();
        try {
            while (parent != null) {
                ret = (ComponentContainer) parent;
                parent = parent.getComponentParent();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return ret;
    }
    public void show(ConfigBean bean){
        this.bean = bean;
        ComponentContainer view = null;
        if(bean.dialog !=null){
            // TODO getDecorView
            view = (ComponentContainer)bean.dialog.getContentCustomComponent();
        }else if(bean.alertDialog !=null){
            view = (ComponentContainer)bean.alertDialog.getContentCustomComponent();
        }
        if(view!=null){
            setUIContent(view);
            WindowAdjuster.adjust(getWindow(),bean);
            bean.listener.onShow();
        }else {
            terminateAbility();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(bean!=null && bean.homeKeyReceiver!=null){
            try {
                CommonEventManager.unsubscribeCommonEvent(bean.homeKeyReceiver);
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }

    @Override
    public void onBackPressed() {

        if(bean.cancelable){
            super.onBackPressed();
        }

    }
}
